package cn.zl.common.manage.dashboard;

import com.alibaba.fastjson2.JSONObject;
import oshi.SystemInfo;

public abstract class BaseDashboard {

    protected static final int NUM = 30;

    private static final long KB = 1024;
    private static final long MB = KB * 1024;
    private static final long GB = MB * 1024;
    private static final long TB = GB * 1024;

    protected SystemInfo si;

    protected BaseDashboard(SystemInfo si){
        this.si = si;
    }

    /**
     * 大小字节转换
     *
     * @param size 大小
     * @return 转换后的字符串
     */
    String convert(long size) {
        if (size >= TB) {
            return String.format("%.1f TB", (float) size / TB);
        } else if (size >= GB) {
            return String.format("%.1f GB", (float) size / GB);
        } else if (size >= MB) {
            float f = (float) size / MB;
            return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
        } else if (size >= KB) {
            float f = (float) size / KB;
            return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
        } else {
            return String.format("%d B", size);
        }
    }

    String convertGB(long size) {
        return String.format("%.1f", (float) size / GB);
    }

    @Override
    public String toString() {
        JSONObject json = this.encode();
        return json.toJSONString();
    }

    public abstract JSONObject encode();

    public interface IRate{
        JSONObject encode();
    }


}
